﻿namespace BM.Tools.WebControls.MForm.Generator
{
    using System;
    using System.Xml;

    /// <summary>
    /// A class that takes prefix, local name and text representation from qualified name.
    /// </summary>
    public class QualifiedNameTextCreator
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="QualifiedNameTextCreator"/> class.
        /// </summary>
        /// <param name="qualifiedName">
        /// The qualified name.
        /// </param>
        /// <param name="builder">
        /// The builder.
        /// </param>
        public QualifiedNameTextCreator(XmlQualifiedName qualifiedName, FormBuilder builder)
        {
            QualifiedName = qualifiedName;
            Builder = builder;
        }

        /// <summary>
        /// Gets the xml qualified name.
        /// </summary>
        public XmlQualifiedName QualifiedName
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets the form builder.
        /// </summary>
        public FormBuilder Builder
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets the qualified name text representation.
        /// </summary>
        public string QualifiedNameText
        {
            get
            {
                return String.IsNullOrEmpty(Prefix) ? LocalName : string.Concat(Prefix, ":", LocalName);
            }
        }

        /// <summary>
        /// Gets qualified name prefix.
        /// </summary>
        public string Prefix
        {
            get
            {
                if (Namespace == null)
                {
                    return null;
                }

                return Builder.Manager.LookupPrefix(Namespace) ?? Builder.RegisterNewPrefix(Namespace);
            }
        }

        /// <summary>
        /// Gets qualified name namespace.
        /// </summary>
        public string Namespace
        {
            get
            {
                return QualifiedName.Namespace;
            }
        }

        /// <summary>
        /// Gets qualified name local name.
        /// </summary>
        public string LocalName
        {
            get
            {
                return QualifiedName.Name;
            }
        }
    }
}
